﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;


namespace DBTools.DBUpdate
{
    public partial class IniSetForm : FormBase
    {
        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        public IniSetForm()
        {
            InitializeComponent();
        }

        private void SettingForm_Load(object sender, EventArgs e)
        {
            //固定窗口大小
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            SetData();
        }

        private void button_Canel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button_OK_Click(object sender, EventArgs e)
        {
            SaveData();
            MessageBox.Show("更新成功，请关闭程序再次启动!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            this.Close();
        }
        public void SaveData()
        {
            //更新配置
            string pathstr = ReturnLoginPath() + "\\Constr.ini";
            string key = "infomedia.com.cn";
            if (!File.Exists(pathstr))
            {
                File.Create(pathstr);
            }
            else
            {
                StringBuilder temp = new StringBuilder(255);
                int it = GetPrivateProfileString("SystemInfo", "key", "null", temp, 255, pathstr);
                key = temp.ToString();
            }
            //long i = WritePrivateProfileString("Server", "key", CryptoHelper.Encrypt(textBox_server.Text, key), pathstr);
            //i = WritePrivateProfileString("UserName", "key", CryptoHelper.Encrypt(textBox_username.Text, key), pathstr);
            //i = WritePrivateProfileString("Password", "key", CryptoHelper.Encrypt(textBox_password.Text, key), pathstr);
            //i = WritePrivateProfileString("DataBase", "key", CryptoHelper.Encrypt(textBox_database.Text, key), pathstr);
            //i = WritePrivateProfileString("WebService", "key", textBox_webservice.Text, pathstr);
           
        }
        public void SetData()
        {
            if (File.Exists(ReturnLoginPath() + "\\Constr.ini"))
            {
                //StringBuilder temp = new StringBuilder(255);
                //int i = GetPrivateProfileString("SystemInfo", "key", "null", temp, 255, ReturnLoginPath() + "\\Constr.ini");
                //string key = temp.ToString();

                //i = GetPrivateProfileString("Server", "key", "null", temp, 255, ReturnLoginPath() + "\\Constr.ini");
                //if (temp != null)
                //{
                //    textBox_server.Text = CryptoHelper.Decrypt(temp.ToString(), key);
                //}

                //i = GetPrivateProfileString("UserName", "key", "null", temp, 255, ReturnLoginPath() + "\\Constr.ini");
                //if(temp!=null)
                //{
                //    textBox_username.Text = CryptoHelper.Decrypt(temp.ToString(), key);
                //}

                //i = GetPrivateProfileString("Password", "key", "null", temp, 255, ReturnLoginPath() + "\\Constr.ini");
                //if (temp != null)
                //{
                //    textBox_password.Text = CryptoHelper.Decrypt(temp.ToString(), key);
                //}

                //i = GetPrivateProfileString("DataBase", "key", "null", temp, 255, ReturnLoginPath() + "\\Constr.ini");
                //if (temp != null)
                //{
                //    textBox_database.Text = CryptoHelper.Decrypt(temp.ToString(), key);
                //}
                //i = GetPrivateProfileString("WebService", "key", "null", temp, 255, ReturnLoginPath() + "\\Constr.ini");
                //textBox_webservice.Text= temp.ToString();
                //i = GetPrivateProfileString("DataFlag", "key", "null", temp, 255, ReturnLoginPath() + "\\Constr.ini");
                //if (temp.ToString().Trim() == "0")
                //{
                //    radioButton_database.Checked = true;
                //}
                //if (temp.ToString().Trim() == "1")
                //{
                //    radioButton_webservice.Checked = true;
                //}  
              
            } 
        }
        public static string ReturnLoginPath()
        {
            return Environment.CurrentDirectory;

        }

        private void radioButton_database_CheckedChanged(object sender, EventArgs e)
        {
            //radioCheck();
        }

        private void radioButton_webservice_CheckedChanged(object sender, EventArgs e)
        {
            //radioCheck();
        }
        private void radioCheck()
        {
            //if (radioButton_database.Checked)
            //{
            //    textBox_webservice.Enabled = false;
            //}
            //else
            //{
            //    textBox_database.Enabled = true;
            //    textBox_password.Enabled = true;
            //    textBox_server.Enabled = true;
            //    textBox_username.Enabled = true;
            //} 
            //if (radioButton_webservice.Checked)
            //{
            //    textBox_database.Enabled = false;
            //    textBox_password.Enabled = false;
            //    textBox_server.Enabled = false;
            //    textBox_username.Enabled = false;
            //}
            //else
            //{
            //    textBox_webservice.Enabled = true;
            //}
        }

        private void button_TestOrderWebService_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("测试前会保所有修改配置，是否继续？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                return;
            }
            SaveData();
            try 
            {
                string sql = "select * from sysobjects";
                //服务器取值
                
                //DataSet myds = Temp.ReturnDataSet(username, password, sql);
                //if (myds == null)
                //{
                //    MessageBox.Show("查询结果返回为空!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                //}
                //else
                //{
                //    MessageBox.Show("查询成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }
    }
}
